using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Tickets;

namespace AndyPike.Unfuddle.Commands.Tickets
{
    /// <summary>
    /// Provides a command for creating a new ticket on the Unfuddle server
    /// </summary>
    public class CreateTicket : IUnfuddleChangeCommand
    {
        private readonly Ticket ticket;

        /// <summary>
        /// Instantiates the CreateTicket command
        /// </summary>
        /// <param name="ticket">The new ticket to create</param>
        public CreateTicket(Ticket ticket)
        {
            this.ticket = ticket;
        }

        /// <summary>
        /// Creates a new ticket on the Unfuddle server
        /// </summary>
        /// <param name="service">The Unfuddle service to use when executing the command</param>
        public void Execute(IUnfuddleService service)
        {
            GuardAgainst.ArgumentNull(service, "service");

            ticket.Validate(EntityValidationContext.Creating);

            string url = service.BuildUrl("projects/{0}/tickets", ticket.ProjectId);
            service.InvokeRequest(url, HttpMethod.POST, ticket);
        }
    }
}